package com.yang.guliproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.guliproduct.entity.Category.OneCategory;
import com.yang.guliproduct.entity.Category.ThreeCategory;
import com.yang.guliproduct.entity.Category.TwoCategory;
import com.yang.guliproduct.entity.PmsCategory;
import com.yang.guliproduct.entity.PmsCategoryBrandRelation;
import com.yang.guliproduct.mapper.PmsCategoryMapper;
import com.yang.guliproduct.service.PmsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements PmsCategoryService {

    @Autowired
    private PmsCategoryBrandRelationServiceImpl pmsCategoryBrandRelationService;

    @Override
    public List<OneCategory> getCategory() {
        //一级分类
        QueryWrapper<PmsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cat_level",1);
        List<PmsCategory> pmsCategories = baseMapper.selectList(queryWrapper);
        //二级分类
        QueryWrapper<PmsCategory> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("cat_level",2);
        List<PmsCategory> pmsCategories1 = baseMapper.selectList(queryWrapper1);
        //三级分类
        QueryWrapper<PmsCategory> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("cat_level",3);
        List<PmsCategory> pmsCategories2 = baseMapper.selectList(queryWrapper2);
        //用于封装最终分类数据
        ArrayList<OneCategory> finalarrayList = new ArrayList<>();


        for (int i = 0; i < pmsCategories.size(); i++) {
            PmsCategory pmsCategory = pmsCategories.get(i);
            OneCategory oneCategory = new OneCategory();
            BeanUtils.copyProperties(pmsCategory,oneCategory);
            finalarrayList.add(oneCategory);
            ArrayList<TwoCategory> objects = new ArrayList<>();

            for (int i1 = 0; i1 < pmsCategories1.size(); i1++) {
                TwoCategory twoCategory = new TwoCategory();
                ArrayList<ThreeCategory> list = new ArrayList<>();
                PmsCategory pmsCategory1 = pmsCategories1.get(i1);
                if (pmsCategory1.getParentCid().equals(pmsCategory.getCatId())){
                    BeanUtils.copyProperties(pmsCategory1,twoCategory);
                    objects.add(twoCategory);
                }

                for (int i2 = 0; i2 < pmsCategories2.size(); i2++) {

                    PmsCategory pmsCategory2 = pmsCategories2.get(i2);
                    if (pmsCategory2.getParentCid().equals(pmsCategory1.getCatId())){
                        ThreeCategory threeCategory = new ThreeCategory();
                        BeanUtils.copyProperties(pmsCategory2,threeCategory);

                        list.add(threeCategory);
                    }

                }
                oneCategory.setChildren(objects);
                twoCategory.setChildren(list);
            }
        }
        return finalarrayList;
    }

    @Override
    public Boolean upCategoryBrand(PmsCategory pmsCategory) {
        String categoryName = pmsCategory.getName();
        Long catId = pmsCategory.getCatId();
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",catId);
        List<PmsCategoryBrandRelation> brandRelations = pmsCategoryBrandRelationService.list(queryWrapper);
        brandRelations.get(0).setCatelogName(categoryName);
        boolean batch = false;
        if (!StringUtils.isEmpty(categoryName)){
            batch = pmsCategoryBrandRelationService.updateBatchById(brandRelations);
        }
        return batch;
    }

    @Override
    public Boolean deleteCategoryBrand(Long id) {
        QueryWrapper<PmsCategoryBrandRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catelog_id",id);
        boolean remove = pmsCategoryBrandRelationService.remove(queryWrapper);
        return remove;
    }
}
